﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Synergy
{
    public partial class FormRkqk2 : Form
    {
        public FormRkqk2()
        {
            InitializeComponent();
        }

        private void button刷新_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("007002001"))
            {
                List入库();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
        }

        private void List入库()
        {
            string sql;
            ///最大顺序号的报工单，未入库的
            sql = @" select '' 行号,mom_order.mocode 生产订单号, mom_order.createdate 生产订单日期,mom_orderdetail.invcode 物料编号,Inventory.cinvname 物料名称,
            qty 生产订单数量, sum(rdrecords10.iquantity) 入库数量, qty-sum(rdrecords10.iquantity) 未入库数量 --,  rdrecord10.ccode 入库单号,rdrecord10.ddate 入库时间 
            from mom_order  join mom_orderdetail 
            on mom_orderdetail.moid=mom_order.moid join Inventory on Inventory.cinvcode=mom_orderdetail.invcode 
            join rdrecord10 on rdrecord10.cMPoCode=mom_order.mocode join rdrecords10 on rdrecord10.ID =rdrecords10.ID   where 1=1  ";

            if (this.textBox1.Text != "")
            {
                sql += " and (mom_orderdetail.invcode like '%" + this.textBox1.Text + "%' or Inventory.cinvname like '%" + this.textBox1.Text + "%' ) ";
            }


            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and rdrecord10.ddate>='" + this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd") + "'";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and rdrecord10.ddate<'" + this.dateTimePicker_jsrq.Value.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }

            sql += " and  mom_orderdetail.invcode=rdrecords10.cinvcode and  rdrecord10.cMPoCode=mom_order.mocode ";


            sql += " group by mom_order.mocode,mom_order.createdate,mom_orderdetail.invcode ,Inventory.cinvname, qty   order by mom_order.mocode  ";
            DataTable tb = pub.U8GetTable(sql);
            this.dataGridView1.AutoGenerateColumns = false;

            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                }
            }

            this.dataGridView1.DataSource = tb;
            this.dataGridView1.Refresh();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //if (pub.是否有权限("007002002"))
            //{
            //    string fileName = "";
            //    string saveFileName = "";
            //    SaveFileDialog saveDialog = new SaveFileDialog();
            //    saveDialog.DefaultExt = "xls";
            //    saveDialog.Filter = "Excel文件|*.xls";
            //    saveDialog.FileName = fileName;
            //    saveDialog.ShowDialog();
            //    saveFileName = saveDialog.FileName;
            //    if (saveFileName.IndexOf(":") < 0) return; //被点了取消
            //    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            //    if (xlApp == null)
            //    {
            //        MessageBox.Show("无法创建Excel对象，您的电脑可能未安装Excel");
            //        return;
            //    }
            //    Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            //    Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            //    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 
            //    //写入标题             
            //    for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //    { worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; }
            //    //写入数值
            //    for (int r = 0; r < dataGridView1.Rows.Count; r++)
            //    {
            //        for (int i = 0; i < dataGridView1.ColumnCount; i++)
            //        {
            //            worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value;
            //        }
            //        System.Windows.Forms.Application.DoEvents();
            //    }
            //    worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            //    MessageBox.Show(fileName + "资料保存成功", "提示", MessageBoxButtons.OK);
            //    if (saveFileName != "")
            //    {
            //        try
            //        {
            //            workbook.Saved = true;
            //            workbook.SaveCopyAs(saveFileName);  //fileSaved = true;                 
            //        }
            //        catch (Exception ex)
            //        {//fileSaved = false;                      
            //            MessageBox.Show("导出文件时出错,文件可能正被打开！\n" + ex.Message);
            //        }
            //    }
            //    xlApp.Quit();
            //    GC.Collect();//强行销毁           }

            //}
            //else
            //{
            //    MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            //}
        }
    }
}
